#include "stdafx.h"
#include "Camera.h"
#define DEG2RAD(x) 3.142*x/180

CCamera::CCamera(void)
{

}


CCamera::~CCamera(void)
{
}

void
CCamera::AddedToGameObject()
{
	//tag this object with 'Camera'
	CTagManager::Instance()->AddTag(m_pGameObject,"Camera");

	assert (m_pGameObject);
	//switch on terrain following
	m_pGameObject->GetTransform()->m_bClampToTerrain=true;
	m_pGameObject->GetTransform()->m_fClampOffset=1.0f;
}

void 
CCamera::Render()
{
	//translate to position specified by x,y,z
	//glTranslatef(0,0,5);
	//glTranslatef(m_pGameObject->GetTransform()->GetPosition().m_fX, m_pGameObject->GetTransform()->GetPosition().m_fY,m_pGameObject->GetTransform()->GetPosition().m_fZ);		// move to proper position
	//gluLookAt(0,0,15,0,0,0,0,1,0);
	//calculate new lookat based on current Yaw,pitch roll
	float yaw=m_pTransform->GetYaw();
	float pitch=m_pTransform->GetPitch();
	float x=m_pTransform->GetPosition().m_fX;
	float y=m_pTransform->GetPosition().m_fY;
	float z=m_pTransform->GetPosition().m_fZ;
	float cosYaw,sinYaw,sinPitch;
	cosYaw=(float)cos(DEG2RAD(yaw));
	sinYaw=(float)sin(DEG2RAD(yaw));
	sinPitch=(float)sin(DEG2RAD(pitch));

	//update lookat based on yaw and pitch
	float lookAtx=x+sinYaw;
	float lookAty=y+sinPitch;
	float lookAtz=z-cosYaw;

	gluLookAt(x,y,z,lookAtx, lookAty, lookAtz,0,1,0);
	s_vCameraPos.m_fX = x;
	s_vCameraPos.m_fY = y;
	s_vCameraPos.m_fZ = z;

}


CVector3 CCamera::s_vCameraPos;